package com.dc.trans.steps.uncompress;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.pentaho.di.core.CheckResult;
import org.pentaho.di.core.CheckResultInterface;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.Counter;
import org.pentaho.di.core.RowMetaAndData;
import org.pentaho.di.core.SQLStatement;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleDatabaseException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.exception.KettleXMLException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.util.StringUtil;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.xml.XMLHandler;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.shared.SharedObjectInterface;
import org.pentaho.di.trans.DatabaseImpact;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;
import org.w3c.dom.Node;

import com.dc.di.exchange.database.ExDatabase;
import com.dc.trans.steps.compress.Compress;

public class UncompressMeta extends BaseStepMeta implements StepMetaInterface {
	
	private static Class<?> PKG = UncompressMeta.class;
	private String uncompressmode;
	 private String fieldToUse;
	private String uncompressTarget;
	
	@Override
	public void readRep(Repository rep, ObjectId id_step, List<DatabaseMeta> databases, Map<String, Counter> counters)
			throws KettleException {
		try {
			uncompressmode= rep.getStepAttributeString(id_step, "uncompress_mode");
			fieldToUse= rep.getStepAttributeString(id_step, "field_to_use");
			uncompressTarget= rep.getStepAttributeString(id_step, "uncompress_target");
		} catch (Exception e) {
			throw new KettleException("Unexpected error reading step information from the repository", e);
		}
		
	}
	
	@Override
	public void saveRep(Repository rep, ObjectId id_transformation,
			ObjectId id_step) throws KettleException {
		try {
			rep.saveStepAttribute(id_transformation, id_step, "uncompress_mode", uncompressmode);
			rep.saveStepAttribute(id_transformation, id_step, "field_to_use", fieldToUse);
			rep.saveStepAttribute(id_transformation, id_step, "uncompress_target", uncompressTarget);
		} catch (Exception e) {
			throw new KettleException("Unable to save step information to the repository for id_step=" + id_step, e);
		}
		
	}
	
	public void loadXML(Node stepnode, List<DatabaseMeta> databases, Map<String, Counter> counters)
			throws KettleXMLException {
		readData(stepnode, databases);
	}

	public Object clone() {
		UncompressMeta retval = (UncompressMeta) super.clone();
		return retval;
	}
	/**
	 * @param useBatchUpdate
	 *            The useBatchUpdate flag to set.
	 */
	private void readData(Node stepnode, List<? extends SharedObjectInterface> databases) throws KettleXMLException {
		try {
			uncompressmode = XMLHandler.getTagValue(stepnode, "uncompress_mode");
			fieldToUse = XMLHandler.getTagValue(stepnode, "field_to_use");
			uncompressTarget = XMLHandler.getTagValue(stepnode, "uncompress_target");
		} catch (Exception e) {
			throw new KettleXMLException("Unable to load step info from XML", e);
		}
	}

	public void setDefault() {
		fieldToUse = "gt3xml";
	}

	public String getXML() {
		StringBuffer retval = new StringBuffer();
		retval.append("    " ).append( XMLHandler.addTagValue("uncompress_mode", uncompressmode));
		retval.append("    " ).append( XMLHandler.addTagValue("field_to_use", fieldToUse));
		retval.append("    " ).append( XMLHandler.addTagValue("uncompress_target", uncompressTarget));
		return retval.toString();
	}
	

	public void getFields(RowMetaInterface row, String origin, RowMetaInterface[] info, StepMeta nextStep,
			VariableSpace space) throws KettleStepException {
		List<CheckResultInterface> remarks = new ArrayList<CheckResultInterface>();
		RowMetaAndData rowMetaAndData = Uncompress.buildRow(this, remarks, origin);
		if (!remarks.isEmpty()) {
			StringBuffer stringRemarks = new StringBuffer();
			for (CheckResultInterface remark : remarks) {
				stringRemarks.append(remark.toString()).append(Const.CR);
			}
			throw new KettleStepException(stringRemarks.toString());
		}
		
		for (ValueMetaInterface valueMeta : rowMetaAndData.getRowMeta().getValueMetaList()) {
			valueMeta.setOrigin(origin);
		}
		
		row.mergeRowMeta(rowMetaAndData.getRowMeta());
	}

	public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta,
			RowMetaInterface prev, String input[], String output[], RowMetaInterface info) {

		// See if we have input streams leading to this step!
		if (input.length > 0) {
			CheckResult cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG,"CompressMeta.CheckResult.ExpectedInputOk"), stepMeta);
			remarks.add(cr);
		} else {
			CheckResult cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG,"CompressMeta.CheckResult.ExpectedInputError"), stepMeta);
			remarks.add(cr);
		}
	}

	
	public StepInterface getStep(StepMeta stepMeta, StepDataInterface stepDataInterface, int cnr, TransMeta transMeta,
			Trans trans) {
		return new Uncompress(stepMeta, stepDataInterface, cnr, transMeta, trans);
	}

	public StepDataInterface getStepData() {
		return new UncompressData();
	}

	public boolean supportsErrorHandling() {
		return true;
	}

	public String getUncompressmode() {
		return uncompressmode;
	}

	public void setUncompressmode(String compressmode) {
		this.uncompressmode = compressmode;
	}

	public String getUncompressTarget() {
		return uncompressTarget;
	}

	public void setUncompressTarget(String compressTarget) {
		this.uncompressTarget = compressTarget;
	}
	 /**
     * @return Returns the fieldToUse.
     */
    public String getFieldToUse()
    {
        return fieldToUse;
    }

    /**
     * @param fieldToUse The fieldToUse to set.
     */
    public void setFieldToUse(String fieldToUse)
    {
        this.fieldToUse = fieldToUse;
    }
}
